//
// Created by zzc on 17-12-26.
//

#include "adlist.h"
#include "zmalloc.h"
#include <stdlib.h>

list *listCreate(void)
{
    list *l;

    if((l = zmalloc(sizeof(list))) == NULL) {
        return NULL;
    }

    l->len = 0;
    l->head = l->tail = NULL;
    l->free = NULL;
    l->dup = NULL;
    l->match = NULL;
    return l;
}

void listRelease(list *list)
{
    unsigned  long len;
    listNode *next;
    listNode *current = list->head;
    len = list->len;
    while (len--) {
        next = current->next;
        if(list->free) list->free(current->value);

        zfree(current);
        current = next;
    }
    zfree(list);
}

list *listAddNodeHead(list *list, void *value)
{
    listNode *node;

    if((node = zmalloc(sizeof(listNode))) == NULL) {
        return NULL;
    }

    node->value = value;

    if(list->len == 0) {
        list->head = list->tail = node;
        node->next = node->prev = NULL;

    } else {
        node->prev = NULL;
        node->next = list->head;
        list->head->prev = node;
        list->head = node;
    }
    list->len++;
    return list;

}

list *listAddNodeTail(list *list, void *value)
{
    listNode *node;

    if((node = zmalloc(sizeof(listNode))) == NULL) {
        return NULL;
    }

    node->value = value;

    if(list->len == 0) {
        list->head = list->tail = node;
        node->next = node->prev = NULL;

    } else {
        node->next = NULL;
        node->prev = list->tail;
        list->tail->next = node;
        list->tail = node;
    }
    list->len++;
    return list;
}

list *listInsertNode(list *list, listNode *old_node, void *value, int after)
{
    listNode *node;

    if((node = zmalloc(sizeof(listNode))) == NULL) {
        return list;
    }

    node->value = value;

    if(after) {
        node->next = old_node->next;
        node->prev = old_node;
        if(list->tail == old_node) {
            list->tail = node;
        }
    } else {
        node->next = old_node;
        node->prev = old_node->prev;
        if(list->head == old_node) {
            list->head = node;
        }
    }

    if(node->prev != NULL) {
        node->prev->next = node;
    }

    if(node->next != NULL) {
        node->next->prev = node;
    }

    list->len++;
    return list;
}

void listDelNode(list *list, listNode *node) {
    if (node->prev)
        node->prev->next = node->next;
    else
        list->head = node->next;

    if (node->next)
        node->next->prev = node->prev;
    else
        list->tail = node->prev;

    if (list->free) list->free(node->value);

    zfree(node);

    list->len--;

}

listIter *listGetIterator(list *list, int direction)
{
    listIter *iter = zmalloc(sizeof(listIter));
    if(iter == NULL)
        return NULL;
    iter->direction = direction;
    if(iter->direction == AL_START_HEAD) {
        iter->next = list->head;
    } else {
        iter->next = list->tail;
    }
    return iter;
}
listNode *listNext(listIter *iter)
{
    listNode *next = iter->next;

    if(next != NULL) {
        if(iter->direction == AL_START_HEAD) {
            iter->next = next->next;
        } else {
            iter->next = next->prev;
        }
    }

    return next;
}

void listReleaseIterator(listIter *iter)
{
    zfree(iter);
}

void listRewind(list *list, listIter *li)
{
    li->next = list->head;
    li->direction = AL_START_HEAD;
}

void listRewindTail(list *list, listIter *li)
{
    li->next = list->tail;
    li->direction = AL_START_TAIL;
}

list *listDup(list *orig)
{
    list *copy;
    listIter *iter;
    listNode *node;

    if((copy = zmalloc(sizeof(list))) == NULL)
        return NULL;

    iter = listGetIterator(orig, AL_START_HEAD);
    while((node = listNext(iter)) != NULL) {
        void *value;

        // 复制节点值到新节点
        if (copy->dup) {
            value = copy->dup(node->value);
            if (value == NULL) {
                listRelease(copy);
                listReleaseIterator(iter);
                return NULL;
            }
        } else
            value = node->value;
        if(listAddNodeTail(copy, value) == NULL) {
            listRelease(copy);
            listReleaseIterator(iter);
            return NULL;
        }
    }

    listReleaseIterator(iter);
    return copy;
}

listNode *listIndex(list *list, long index)
{
    listIter *iter = listGetIterator(list, index >= 0 ? AL_START_HEAD : AL_START_TAIL);
    if(index >= 0) {
        index++;
    } else {
        index = 0 - index;
    }
    listNode *node = NULL;
    while (index--) {
        node = listNext(iter);
        if(node == NULL)
            goto end;
    }


    end:
    listReleaseIterator(iter);
    return node;
}

listNode *listSearchKey(list *list, void *key)
{
    listIter *iter = listGetIterator(list, AL_START_HEAD);
    listNode *node;
    while((node = listNext(iter)) != NULL) {
        if(list->match) {
         if(list->match(node->value, key))
             goto end;
        } else {
            if(node->value == key)
                goto end;
        }
    }

    end:
    listReleaseIterator(iter);
    return node;
}

void listRotate(list *list)
{
    listNode *tail = list->tail;

    if (listLength(list) <= 1) return;

    list->tail = tail->prev;
    list->head->prev = tail;
    tail->next = list->head;
    list->head = tail;
    tail->prev = NULL;
}